package io.renren.modules.sys.web_api.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import io.renren.common.utils.Stringutils;
import io.renren.modules.sys.back.entity.*;
import io.renren.modules.sys.back.service.*;
import io.renren.modules.sys.entity.UserEntity;
import io.renren.modules.sys.service.UserService;
import io.renren.modules.sys.web_api.service.FirstPageService;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Service("firstPageService")
public class FirstPageServiceimpl implements FirstPageService {
    @Autowired
    private UserService userService;
    @Autowired
    private BtCtBaseService btCtBaseService;
    @Autowired
    private BtCtGaishuService btCtGaishuService;
    @Autowired
    private BtCategroyManageService btCategroyManageService;
    @Autowired
    private  BtJishuInfoService btJishuInfoService;
    @Autowired
    private BtWendaInfoService btWendaInfoService;
    @Autowired
    private BtPingpaiInfoService btPingpaiInfoService;
    @Autowired
    private BtRenwuInfoService btRenwuInfoService;
    @Override
    public List<Map<String,Object>> firstPageLunBo(){

       List<Map<String,Object>>  datalist = new ArrayList<>();
        Collection<String> c = new ArrayList();
        c.add("tjsj");
        List<BtCtBaseEntity> ctbeanlist = btCtBaseService.selectList(new EntityWrapper<BtCtBaseEntity>().orderDesc(c));
        int cont=0;
        for(int i=0 ;i<ctbeanlist.size();i++){
            Map<String,Object> data = new HashMap<>();
           if(cont>6)break;
            BtCtGaishuEntity btCtGaishuEntity= btCtGaishuService.selectById(ctbeanlist.get(i).getGsid());
            if(btCtGaishuEntity==null) continue;
            if(!Stringutils.IsNULLorEmpty(btCtGaishuEntity.getContent()))continue;
            String tmp = btCtGaishuEntity.getContent();
            if(!tmp.equals("")){
                data.put("name",ctbeanlist.get(i).getName());
                data.put("img",ctbeanlist.get(i).getImg());
                data.put("link","/api/product/detail/"+ctbeanlist.get(i).getId());
                Document doc = Jsoup.parse(tmp);
                Elements subimg = doc.getElementsByTag("p");
                if(subimg!=null&&subimg.size()>0) {
                    data.put("content",subimg.get(0).html());
                }
                datalist.add(data);
                cont++;
            }
        }
        return datalist;
    }
    @Override
    public List<Map<String,Object>> firstPageTuiJian(){
        List<Map<String,Object>>  datalist = new ArrayList<>();
        Collection<String> c = new ArrayList();
        c.add("tjsj");
        List<BtCtBaseEntity> ctbeanlist = btCtBaseService.selectList(new EntityWrapper<BtCtBaseEntity>().orderDesc(c).eq("sftj",1));

        for(int i=0 ;i<ctbeanlist.size();i++){
            Map<String,Object> data = new HashMap<>();

            BtCtGaishuEntity btCtGaishuEntity= btCtGaishuService.selectById(ctbeanlist.get(i).getGsid());
            if(btCtGaishuEntity==null) continue;
            if(!Stringutils.IsNULLorEmpty(btCtGaishuEntity.getContent()))continue;
            String tmp = btCtGaishuEntity.getContent();
            if(!tmp.equals("")){
                data.put("name",ctbeanlist.get(i).getName());
                data.put("img",ctbeanlist.get(i).getImg());
                data.put("link","/api/product/detail/"+ctbeanlist.get(i).getId());
                Document doc = Jsoup.parse(tmp);
                Elements subimg = doc.getElementsByTag("p");
                if(subimg!=null&&subimg.size()>0) {
                    data.put("content",subimg.get(0).html());
                }
                datalist.add(data);
            }
        }
        return datalist;
    }
    @Override
    public List<Map<String,Object>> firstPageProFenLei(){
        List<Map<String,Object>>  datalist = new ArrayList<>();
        List<BtCategroyManageEntity> ctbeanlist = btCategroyManageService.selectList(new EntityWrapper<BtCategroyManageEntity>().eq("pid",0));
        for(BtCategroyManageEntity sb:ctbeanlist){
            Map<String,Object> data = new HashMap<>();
            data.put("name",sb.getName());
            data.put("link","/api/product/catag/"+sb.getId());
            datalist.add(data);
        }
        return datalist;
    }
    @Override
    public List<Map<String,Object>> firstPageJiShu(){
        List<Map<String,Object>>  datalist = new ArrayList<>();
        Collection<String> c = new ArrayList();
        c.add("llcs");
        List<BtJishuInfoEntity> ctbeanlist = btJishuInfoService.selectList(new EntityWrapper<BtJishuInfoEntity>().orderDesc(c));
        int cont=0;
        for(BtJishuInfoEntity sb:ctbeanlist){
            Map<String,Object> data = new HashMap<>();
            data.put("name",sb.getName());
            data.put("link","/api/jishu/detail/"+sb.getId());
            String tmp = sb.getSummer();
            if(!tmp.equals("")){
                Document doc = Jsoup.parse(tmp);
                Elements subimg = doc.getElementsByClass("infozyao");
                if(subimg!=null&&subimg.size()>0) {
                    data.put("content",subimg.get(0).childNode(4).outerHtml());
                }
                datalist.add(data);
            }
        }
        return datalist;
    }
    @Override
    public List<Map<String,Object>> firstPageWenDa(){
        List<Map<String,Object>>  datalist = new ArrayList<>();
        Collection<String> c = new ArrayList();
        c.add("llcs");
        List<BtWendaInfoEntity> ctbeanlist = btWendaInfoService.selectList(new EntityWrapper<BtWendaInfoEntity>().orderDesc(c));
        int cont=0;
        for(int i=0;i<ctbeanlist.size();i++){
            if(cont>4)break;
            Map<String,Object> data = new HashMap<>();
            data.put("name",ctbeanlist.get(i).getName());
            data.put("img",ctbeanlist.get(i).getImg());
            data.put("link","/api/wenda/detail/"+ctbeanlist.get(i).getId());
            String tmp = ctbeanlist.get(i).getSummer();
            if(!tmp.equals("")){
                Document doc = Jsoup.parse(tmp);
                Elements subimg = doc.getElementsByClass("infozyao");
                if(subimg!=null&&subimg.size()>0) {
                    data.put("content",subimg.get(0).childNode(4).outerHtml());
                }
                cont++;
                datalist.add(data);
            }
        }
        return datalist;
    }
    @Override
    public List<Map<String,Object>> firstPagePinpai(){
        List<Map<String,Object>>  datalist = new ArrayList<>();
        Collection<String> c = new ArrayList();
        c.add("llcs");
        List<BtPingpaiInfoEntity> ctbeanlist = btPingpaiInfoService.selectList(new EntityWrapper<BtPingpaiInfoEntity>().orderDesc(c));
        int cont=0;
        for(int i=0;i<ctbeanlist.size();i++){
            if(cont>4)break;
            Map<String,Object> data = new HashMap<>();
            data.put("name",ctbeanlist.get(i).getName());
            data.put("img",ctbeanlist.get(i).getImg());
            data.put("link","/api/pinpai/detail/"+ctbeanlist.get(i).getId());
            String tmp = ctbeanlist.get(i).getSummer();
            if(!tmp.equals("")){
                Document doc = Jsoup.parse(tmp);
                Elements subimg = doc.getElementsByClass("infozyao");
                if(subimg!=null&&subimg.size()>0) {
                    data.put("content",subimg.get(0).childNode(4).outerHtml());
                }
                cont++;
                datalist.add(data);
            }
        }
        return datalist;
    }
    @Override
    public List<Map<String,Object>> firstPageRenWu(){
        List<Map<String,Object>>  datalist = new ArrayList<>();
        Collection<String> c = new ArrayList();
        c.add("llcs");
        List<BtRenwuInfoEntity> ctbeanlist = btRenwuInfoService.selectList(new EntityWrapper<BtRenwuInfoEntity>().orderDesc(c));
        int cont=0;
        for(int i=0;i<ctbeanlist.size();i++){
            if(cont>4)break;
            Map<String,Object> data = new HashMap<>();
            data.put("name",ctbeanlist.get(i).getName());
            data.put("img",ctbeanlist.get(i).getImg());
            data.put("link","/api/answer/detail/"+ctbeanlist.get(i).getId());
            String tmp = ctbeanlist.get(i).getSummer();
            if(!tmp.equals("")){
                Document doc = Jsoup.parse(tmp);
                Elements subimg = doc.getElementsByClass("infozyao");
                if(subimg!=null&&subimg.size()>0) {
                    data.put("content",subimg.get(0).childNode(4).outerHtml());
                }
                cont++;
                datalist.add(data);
            }
        }
        return datalist;
    }
    @Override
    public int getzhongtiaomu(){
      int cts=  btCtBaseService.selectCount(new EntityWrapper<BtCtBaseEntity>());
      int rws=  btRenwuInfoService.selectCount(new EntityWrapper<BtRenwuInfoEntity>());
      int jss=  btJishuInfoService.selectCount(new EntityWrapper<BtJishuInfoEntity>());
      int wds=  btWendaInfoService.selectCount(new EntityWrapper<BtWendaInfoEntity>());
      int pps=  btPingpaiInfoService.selectCount(new EntityWrapper<BtPingpaiInfoEntity>());
      return  1240000+ cts+rws+jss+wds+pps;
    }
    @Override
    public int getrenwumu(){
        int cts=  userService.selectCount(new EntityWrapper<UserEntity>());

        return  340000+ cts;
    }
}
